import requests,re,threading
from tkinter import *
from tkinter.scrolledtext  import ScrolledText
def weChatDD():
	try:

		URL = 'http://dnstest.weixin.qq.com/newdns.cgi?pass_ticket=eSzmK0ihBIFfw6lePHcaxUiD79QTj9gaovbtem72vyy%2BlkFE2Gh%2BsU0b6iA2yYOi'
		HEAD = {
			"Host": "dnstest.weixin.qq.com",
			"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
			"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36 MicroMessenger/6.5.2.501 NetType/WIFI WindowsWechat QBCore/3.43.691.400 QQBrowser/9.0.2524.400",
			"Accept-Language": "zh-CN,zh;q=0.8,en-us;q=0.6,en;q=0.5;q=0.4"
		}
		r = requests.get(url=URL, headers=HEAD,verify=False)
		RES = str(r.text)
		# print(RES)
		wechatRES = re.findall('<tr.+</tr>', RES)[0].replace("<tr>", "").replace("<td>", "").split("</tr>")
		print("wechatRES", wechatRES)
		del wechatRES[0]
		wechatRES.pop()
		wechatDict = {}
		for x in wechatRES:
			wc = x.split("</td>")
			obj = wc[1]
			domain = wc[0]
			ip = wc[3]
			yys = wc[4]
			wechatDict[obj] = (obj, domain, ip, yys)
		for x in wechatDict:
			print("项目：%-20s\t域名：%-20s\tIP：%-20s\tIP：%-20s\n" % wechatDict[x])
			output2.insert(END, "%-20s\t域名：%-20s\tIP：%-20s\t运营商：%-20s\n" % wechatDict[x])
		DD = re.findall("附：微信调度地址,请确保都通过本网地址访问这些调度地址<br>.+<br>", RES)
		DD = DD[0].replace("<br>", "\n")
		sec = re.findall("您的来源IP是[^<]+", RES)[0] + "\n" + DD
		print(sec)
		output1.insert(END, sec)
		var.set("检测完毕")
	except Exception as e:
		print(e)
		output1.insert(END, e)



def workTD():
	var.set("正在检测")
	output1.delete(0.0,END)
	output2.delete(0.0, END)
	th = threading.Thread(target=weChatDD())  # 创建后台线程，执行work函数
	th.setDaemon(True)  # 守护线程
	print("后台线程启动……")
	th.start()

if  __name__ =='__main__':
	root = Tk()
	root.title('微信调度检测')
	#row=0
	var=StringVar()
	l1=Label(root,textvariable=var,fg='red').grid(row=0,column=0, padx=5, pady=5)
	var.set("点击检测")
	B1=Button(root,text="开始检测",command=workTD).grid(row=0,column=1, padx=5, pady=5)
	#row=1
	lo1=Label(root,text="调度信息").grid(row=1,column=0, padx=5, pady=5)
	lo2 = Label(root, text="资源信息").grid(row=1,column=1, padx=5, pady=5)
	#row=2
	output1 = ScrolledText(root, font=('微软雅黑', 10), fg='blue', bg='#F0F0F0', height=25, width=40)
	output1.grid(row=2, column=0, padx=5, pady=5, sticky=N)
	output2 = ScrolledText(root, font=('微软雅黑', 10), fg='blue', bg='#F0F0F0', height=25, width=80)
	output2.grid(row=2, column=1, padx=5, pady=5, sticky=N)
	root.mainloop()

